using System;
using SSOConfig;
using System.Xml.Serialization;
using System.IO;

namespace Sixeyed.CacheAdapter.Configuration
{
    /// <summary>
    /// SSO application store wrapper for accessing cache configuration
    /// </summary>
    public sealed class SSOConfiguration
    {
        private static bool _cacheSSOAccess = false;
        private static object _syncLock = new object();
        private static SSOConfig.SSOHelper _ssoHelper;
        private static SSOConfig.SSOApplication _application;

        private SSOConfiguration() { }

        /// <summary>
        /// Gets/sets whether properties read from SSO are cached
        /// </summary>
        public static bool CacheSSOAccess
        {
            get { return _cacheSSOAccess; }
            set { _cacheSSOAccess = value; }
        }

        private static SSOConfig.SSOHelper SSOHelper
        {
            get
            {
                if (_ssoHelper == null)
                {
                    lock (_syncLock)
                    {
                        if (_ssoHelper == null)
                        {
                            _ssoHelper = new SSOHelper();
                        }
                    }
                }
                return _ssoHelper;
            }
        }

        private static SSOConfig.SSOApplication SSOApplication
        {
            get
            {
                if (_application == null || !_cacheSSOAccess)
                {
                    _application = SSOHelper.GetApplication(Application.Name);
                }
                return _application;
            }
        }

        /// <summary>
        /// Gets the collection of message cache configurations in SSO
        /// </summary>
        public static MessageCacheConfigurationCollection MessageCacheConfigurationCollection
        {
            get
            {
                MessageCacheConfigurationCollection configurationCollection = new MessageCacheConfigurationCollection();
                string configString = (string)SSOApplication.GetFieldValue(Field.MessageCacheConfigurationCollection);
                if (!string.IsNullOrEmpty(configString))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(MessageCacheConfigurationCollection));
                    configurationCollection = (MessageCacheConfigurationCollection)serializer.Deserialize(new StringReader(configString));
                }
                return configurationCollection;
            }
        }

        /// <summary>
        /// Field Name struct
        /// </summary>
        public struct Field
        {
            /// <summary>
            /// "MessageCacheConfigurationCollection"
            /// </summary>
            public const string MessageCacheConfigurationCollection = "MessageCacheConfigurationCollection";
        }

        /// <summary>
        /// Application Info Name struct
        /// </summary>
        public struct Application
        {
            /// <summary>
            /// "Sixeyed.CacheAdapter"
            /// </summary>
            public const string Name = "Sixeyed.CacheAdapter";

            /// <summary>
            /// "Holds configuration for Sixeyed.CacheAdapter"
            /// </summary>
            public const string Description = "Holds configuration for Sixeyed.CacheAdapter";

            /// <summary>
            /// "http://www.codeplex.com/BizTalkCacheAdapter"
            /// </summary>
            public const string ContactInfo = "http://www.codeplex.com/BizTalkCacheAdapter";

            /// <summary>
            /// "BizTalk Application Users"
            /// </summary>
            public const string UserGroup = "BizTalk Application Users";

            /// <summary>
            /// "BizTalk Server Administrators"
            /// </summary>
            public const string AdminGroup = "BizTalk Server Administrators";
        }
    }
}
